package com.retlife.tvshow.ui

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import com.retlife.tvshow.R
import com.retlife.tvshow.global.EventBusMsg
import com.retlife.tvshow.global.EventBusOrder
import org.greenrobot.eventbus.EventBus


class PauseDialog(context: Context) {
    private val dialog: AlertDialog

    init {
        val builder = AlertDialog.Builder(context)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_quit_game, null)
        builder.setView(view)
        dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.apply {
            setCanceledOnTouchOutside(false)
            val window = window
            val decorView = window!!.decorView
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

            //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间

            //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
            //设置导航栏颜
            //设置导航栏颜
            window.navigationBarColor = Color.TRANSPARENT
            //内容扩展到导航栏
            //内容扩展到导航栏
            window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)

        }
        dialog.setOnKeyListener { dialog, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                when (keyCode) {

                    KeyEvent.KEYCODE_DPAD_LEFT -> {
                        EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_QUIT))
                        dialog.dismiss()
                        return@setOnKeyListener true
                    }

                    KeyEvent.KEYCODE_DPAD_CENTER -> {
                        EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_RESUME))
                        dialog.dismiss()
                        return@setOnKeyListener true
                    }

                    KeyEvent.KEYCODE_BACK -> {
                        EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_RESUME))
                        dialog.dismiss()
                        return@setOnKeyListener true
                    }

                    KeyEvent.KEYCODE_DPAD_RIGHT -> {
                        EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_RESTART))
                        dialog.dismiss()
                        return@setOnKeyListener true
                    }
                }
            }
            false
        }
        view.findViewById<Button>(R.id.btn_quit).setOnClickListener { EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_QUIT)) }
        view.findViewById<Button>(R.id.btn_resume).setOnClickListener { EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_RESUME)) }
        view.findViewById<Button>(R.id.btn_restart).setOnClickListener { EventBus.getDefault().post(EventBusMsg(EventBusOrder.ACTION_RESTART)) }
    }

    fun show() {
        dialog.apply {
            val window = window
            window!!.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
            fullScreenImmersive(window.decorView)
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
            show()
        }

    }

    fun dismiss() {
        dialog.dismiss()

    }

    fun isVisible(): Boolean {
        return dialog.isShowing
    }

    fun destroy() {
    }

    private fun fullScreenImmersive(view: View) {
        val uiOptions = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
        view.systemUiVisibility = uiOptions
    }

}